package com.angus.modules.content.mapper;

import com.angus.modules.content.domain.CNContent;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface CNContentMapper extends BaseMapper<CNContent> {


    /**
     * 根据课程编号和单元编号查询内容
     * @param courseCode
     * @param unitNum
     * @return
     */


    @Select("<script>" +
            "SELECT DISTINCT " +
            "            cc.id,su.unit_num,su.course_code,su.is_pay,cc.group_id,cc.group_flag," +
            "            cc.cn_word,cc.cn_pinyin,cc.cn_paraphrase,cc.cn_translation," +
            "            cc.create_by,cc.create_time,cc.update_by,cc.update_time,cc.read_aloud,cc.sort,cc.mp3_content" +
            "             FROM " +
            "            sys_unit su " +
            "                JOIN course_cn_content cc ON su.group_id = cc.group_id AND su.course_code = cc.course_code " +
            "        WHERE " +
            "            su.course_code = #{courseCode} " +
            "          <if test=\"unitNum != null and unitNum > 0\">" +
            "              AND " +
            "              su.unit_num = #{unitNum}" +
            "          </if>" +
            "          AND " +
            "            su.del_flag = '0'" +
            "          AND " +
            "            cc.del_flag = '0'" +
            "          ORDER BY cc.sort asc" +
            "</script>")
    List<CNContent> getContentList(@Param("courseCode") String courseCode, @Param("unitNum") Integer unitNum);


    @Select("<script>" +
            "SELECT DISTINCT " +
            "            cc.id,su.unit_num,su.course_code,su.is_pay,cc.group_id,cc.group_flag," +
            "            cc.cn_word,cc.cn_pinyin,cc.cn_paraphrase,cc.cn_translation," +
            "            cc.create_by,cc.create_time,cc.update_by,cc.update_time,cc.read_aloud,cc.mp3_content" +
            "             FROM " +
            "            sys_unit su " +
            "                JOIN course_cn_content cc ON su.group_id = cc.group_id AND su.course_code = cc.course_code " +
            "        WHERE " +
            "            su.course_code = #{courseCode} " +
            "              AND su.unit_num = #{unitNum} " +
            "              AND su.group_id = #{groupId} " +
            "              AND su.del_flag = '0' " +
            "              AND cc.del_flag = '0' " +
            "</script>")
    List<CNContent> selectContentByGroupId(@Param("courseCode") String courseCode, @Param("unitNum") Integer unitNum, @Param("groupId") Long groupId);

    @Select("<script>" +
            "SELECT DISTINCT " +
            "            cc.id,su.unit_num,su.course_code,su.is_pay,cc.group_id,cc.group_flag," +
            "            cc.cn_word,cc.cn_pinyin,cc.cn_paraphrase,cc.cn_translation," +
            "            cc.create_by,cc.create_time,cc.update_by,cc.update_time,cc.read_aloud,cc.mp3_content" +
            "             FROM " +
            "            sys_unit su " +
            "                JOIN course_cn_content cc ON su.group_id = cc.group_id AND su.course_code = cc.course_code " +
            "        WHERE " +
            "            su.course_code = #{courseCode} " +
            "              AND su.unit_num = #{unitNum} " +
            "              AND cc.group_flag = #{groupFlag} " +
            "              AND su.del_flag = '0' " +
            "              AND cc.del_flag = '0' " +
            "</script>")
    CNContent selectContentByGroupFlag(@Param("courseCode") String courseCode, @Param("unitNum") Integer unitNum, @Param("groupFlag") String groupFlag);

    @Update("UPDATE course_cn_content SET sort = id")
    int updateSortToId();
}
